package com.goose.bmm.controler.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Vibrator;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.goose.bmm.R;
import com.goose.bmm.controler.fragment.SettingAwayTeamFragment;
import com.goose.bmm.controler.fragment.SettingHomeTeamFragment;
import com.goose.bmm.model.business.Constants;
import com.goose.bmm.model.business.DataGlobal;
import com.goose.bmm.model.entity.Player;

public class PlayerDialog extends Dialog implements View.OnClickListener {

	private SettingHomeTeamFragment mFragmentHomeTeam;
	private SettingAwayTeamFragment mFragmentAwayTeam;
	private Context mContext;

	private EditText mEditTextName;
	private EditText mEditTextNumber;
	private EditText mEditTextBirthYear;
	private Spinner mSpinnerPosition;
	private Button mButtonOKAddPlayer;

	private int TYPE_DIALOG;
	private int PLAYER_INDEX;

	public PlayerDialog(SettingHomeTeamFragment homeTeamFragment,
			SettingAwayTeamFragment awayTeamFragment, Context context,
			int type) {
		super(context);
		mFragmentHomeTeam = homeTeamFragment;
		mFragmentAwayTeam = awayTeamFragment;
		mContext = context;
		this.setContentView(R.layout.dialog_add_player);
		TYPE_DIALOG = type;
		getWindow().setLayout(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		this.setCanceledOnTouchOutside(true);
		
		switch (TYPE_DIALOG) {
		case Constants.ADD_PLAYER_DIALOG: {
			setTitle(mContext.getResources().getString(
					R.string.dialog_title_add_player));
			break;
		}
		case Constants.EDIT_PLAYER_DIALOG: {
			setTitle(mContext.getResources().getString(
					R.string.dialog_title_edit_player));
			break;
		}
		}

		mEditTextName = (EditText) this.findViewById(R.id.ed_name_player);
		mEditTextNumber = (EditText) this.findViewById(R.id.ed_number_player);
		mEditTextBirthYear = (EditText) this
				.findViewById(R.id.ed_birth_year_player);
		mSpinnerPosition = (Spinner) findViewById(R.id.sp_position_player);
		mButtonOKAddPlayer = (Button) findViewById(R.id.btn_OK_add_player);

		mButtonOKAddPlayer.setOnClickListener(this);
	}

	public void setupInfo(int index) {
		PLAYER_INDEX = index;
		Player player;
		if (mFragmentHomeTeam != null) {
			player = DataGlobal.homeListPlayer.get(index);
		} else {
			player = DataGlobal.guestListPlayer.get(index);
		}
		mEditTextName.setText(player.getName());
		mEditTextNumber.setText(player.getNumber() + "");
		mEditTextBirthYear.setText(player.getBirthYear() + "");
		String[] position = mContext.getResources().getStringArray(
				R.array.sp_position);
		for (int i = 0; i < position.length; i++) {
			if (position[i].equals(player.getPositions())) {
				mSpinnerPosition.setSelection(i);
				break;
			}
		}
	}

	@Override
	public void onClick(View v) {
		if (mFragmentHomeTeam != null) {
			if (mEditTextName.getText().toString().isEmpty()) {
				Toast.makeText(
						mContext,
						mContext.getResources().getString(
								R.string.toast_enter_player_name),
						Toast.LENGTH_LONG).show();
				Vibrator vibrator = (Vibrator) mContext
						.getSystemService(Context.VIBRATOR_SERVICE);
				vibrator.vibrate(1000);
				return;
			}

			if (mEditTextNumber.getText().toString().isEmpty()) {
				Toast.makeText(
						mContext,
						mContext.getResources().getString(
								R.string.toast_enter_player_number),
						Toast.LENGTH_LONG).show();
				Vibrator vibrator = (Vibrator) mContext
						.getSystemService(Context.VIBRATOR_SERVICE);
				vibrator.vibrate(1000);
				return;
			}

			Player player = new Player();
			player.setName(mEditTextName.getText().toString());
			player.setNumber(Integer.parseInt(mEditTextNumber.getText()
					.toString()));
			player.setPositions(mSpinnerPosition.getSelectedItem().toString());
			player.setBirthYear(Integer.parseInt(mEditTextBirthYear.getText()
					.toString()));

			switch (TYPE_DIALOG) {
			case Constants.ADD_PLAYER_DIALOG: {
				DataGlobal.homeListPlayer.add(player);
				break;
			}
			case Constants.EDIT_PLAYER_DIALOG: {
				DataGlobal.homeListPlayer.set(PLAYER_INDEX, player);
				break;
			}
			}

			mFragmentHomeTeam.refresfListView();
			dismiss();
		} else {
			if (mEditTextName.getText().toString().isEmpty()) {
				Toast.makeText(
						mContext,
						mContext.getResources().getString(
								R.string.toast_enter_player_name),
						Toast.LENGTH_LONG).show();
				Vibrator vibrator = (Vibrator) mContext
						.getSystemService(Context.VIBRATOR_SERVICE);
				vibrator.vibrate(1000);
				return;
			}

			if (mEditTextNumber.getText().toString().isEmpty()) {
				Toast.makeText(
						mContext,
						mContext.getResources().getString(
								R.string.toast_enter_player_number),
						Toast.LENGTH_LONG).show();
				Vibrator vibrator = (Vibrator) mContext
						.getSystemService(Context.VIBRATOR_SERVICE);
				vibrator.vibrate(1000);
				return;
			}

			Player player = new Player();
			player.setName(mEditTextName.getText().toString());
			player.setNumber(Integer.parseInt(mEditTextNumber.getText()
					.toString()));
			player.setPositions(mSpinnerPosition.getSelectedItem().toString());
			player.setBirthYear(Integer.parseInt(mEditTextBirthYear.getText()
					.toString()));

			switch (TYPE_DIALOG) {
			case Constants.ADD_PLAYER_DIALOG: {
				DataGlobal.guestListPlayer.add(player);
				break;
			}
			case Constants.EDIT_PLAYER_DIALOG: {
				DataGlobal.guestListPlayer.set(PLAYER_INDEX, player);
				break;
			}
			}

			mFragmentAwayTeam.refresfListView();
			dismiss();
		}
	}

}
